import axios from 'axios'
import { Message, Spin } from 'iview';
import router from './router'

const BASEURL = "https://www.easy-mock.com/mock/5c2979042140e71d51972e9a/cl3sm"

axios.defaults.withCredentials = true
axios.defaults.baseURL = BASEURL

function startLoading() {
  Spin.show({
    render: (h) => {
      return h('div', [
        h('Icon', {
          'class': 'demo-spin-icon-load',
          props: {
            type: 'ios-loading',
            size: 18
          }
        }),
        h('div', 'Loading')
      ])
    }
  });
}
function endLoading() {
  Spin.hide();
}

// 请求拦截  设置统一header
axios.interceptors.request.use(config => {
  // 加载
  startLoading()
  return config

}, error => {
  return Promise.reject(error)
})

// 响应拦截  401 token过期处理
axios.interceptors.response.use(response => {
  endLoading()
  return response
}, error => {
  Message.error({
    content: error
  })
  return Promise.reject(error)
})
export default axios